/*
 * DiscoveryWantToBuyProvider.java
 * business
 *
 * Created by chentao on 2017/5/12.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.WantToBuyEntity;
import com.yidiandao.business.ui.main.discovery.wanttobuydetail.WantToBuyDetailActivity;
import com.yidiandao.business.ui.widget.view.GridItemDecoration;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.TimeUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import me.drakeet.multitype.ItemViewProvider;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 发现-求购列表-Provider
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/5/12 0012 17:21
 */
public class DiscoveryWantToBuyProvider extends ItemViewProvider<WantToBuyEntity, DiscoveryWantToBuyProvider.ViewHolder> {

    private Activity mActivity;

    public DiscoveryWantToBuyProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_discovery_want_to_buy, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final WantToBuyEntity entity) {
        ImageLoader.loadAvatar(mActivity, holder.ivAvatar, entity.getSex());
        holder.tvShopName.setText(entity.getDpName());
        holder.tvContent.setText(entity.getDesc());
        holder.tvTime.setText(TimeUtils.getRelativeTimeWithSingleUnit(UIUtils.getResources(), TimeUtils.getAndroidMillis(entity.getAddTime()), true));
        holder.tvAddress.setText(entity.getAddress());

        //  图片
        showImages(holder, entity.getPictures());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toWantToBuyDetailActivity(entity.getWantId());
            }
        });
    }

    private void showImages(@NonNull ViewHolder holder, List<String> pictures) {

        MultiTypeAdapter adapter = new MultiTypeAdapter();
        adapter.register(String.class, new DiscoveryWantToBuyImageProvider(mActivity));
        holder.recyclerView.setAdapter(adapter);
        holder.recyclerView.setLayoutManager(new GridLayoutManager(mActivity, 3));

        GridItemDecoration decoration = new GridItemDecoration(3, UIUtils.dp2px(5), false);
        // 避免重复添加
        holder.recyclerView.removeItemDecoration(decoration);
        holder.recyclerView.addItemDecoration(decoration);

        adapter.setItems(pictures);
        adapter.notifyDataSetChanged();
    }

    private void toWantToBuyDetailActivity(String wantId) {
        WantToBuyDetailActivity.actionStart(mActivity, wantId);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivAvatar;
        private TextView tvShopName;
        private TextView tvContent;
        private TextView tvTime;
        private TextView tvAddress;
        private RecyclerView recyclerView;

        public ViewHolder(View itemView) {
            super(itemView);
            tvAddress = (TextView) itemView.findViewById(R.id.tv_address);
            ivAvatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            tvShopName = (TextView) itemView.findViewById(R.id.tv_shop_name);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recycler_view);
        }
    }
}
